﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using NicolasLightDBModel;
using NicolasLight.Web;
using System.Collections.Generic;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactUsService
{
	[OperationContract]
	public List<ContactDTO>GetContacts()
	{
        using (var ctx = new NicolasLightDBEntities())
        {
            List<ContactDTO> contactToReturn = new List<ContactDTO>();

            foreach (var item in ctx.Contact.Where(cu => cu.FirstName.StartsWith("")))
            {
                contactToReturn.Add(new ContactDTO(item.ContactUsID, item.FirstName, item.LastName, item.Email, item.RetypeEmail, item.Comments));
            }

            return contactToReturn;
        }
	}

    [OperationContract]
    public bool SaveContact(ContactDTO contactToSave)
    {
        try
        {
            using (var ctx = new NicolasLightDBEntities())
            {
                ContactUs currentContact = ctx.Contact.First<ContactUs>(s => s.ContactUsID == contactToSave.ContactUsID);
                currentContact.FirstName = contactToSave.FirstName;
                currentContact.LastName = contactToSave.LastName;
                currentContact.Email = contactToSave.Email;
                currentContact.RetypeEmail = contactToSave.RetypeEmail;
                currentContact.Comments = contactToSave.Comments;
                ctx.SaveChanges();
            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}
